﻿/*
 * Author:  hammercui
 * CreateTime: 2018/10/8 15:25:53
 * Module: Implement
 * 主要功能：原生实现，目前只针对android平台
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MGAudioEngine;
using UnityEngine;

namespace MGAudioEngine
{
    public sealed class MGAudioEngineNativeImp : IMGAudioEngine
    {
        private static string JAVA_CLASS = "com.sdbean.audio.unity.MGAudioUnityWrapper";
        private AndroidJavaObject _javaWarpper;
        public void AuthApp(string appId,string appKey,string deviceToken)
        {
#if UNITY_ANDROID
            //获得Activity实例
            if (_javaWarpper == null)
            {
                _javaWarpper = new AndroidJavaClass(JAVA_CLASS);
            }
#endif
            int code = _javaWarpper.CallStatic<int>(WrapperFuction.init.ToString());
            if (code == 1)
            {
                Debuger.Log("android activity init success");
                _javaWarpper.CallStatic(WrapperFuction.authentication.ToString(), appId, appKey, deviceToken);
                MGAudioEngine.Instance.SetConnectState = ConnectState.Authenticating;
            }else{
                Debuger.LogError("can not auth ,because activity not exit");
                MGAudioEngine.Instance.SetConnectState = ConnectState.Init;
            }
        }

        public void InitAduioSetting()
        {

        }

        public void UpdateAudioSetting()
        {

        }

        public void JoinRoom(long roomId,string userName)
        {
            _javaWarpper.CallStatic(WrapperFuction.joinRoom.ToString(), roomId, userName);
        }

        public void QuitRoom(long roomId)
        {
            _javaWarpper.CallStatic(WrapperFuction.quitRoom.ToString(), roomId);
        }

        public void CreateRoom(string userName)
        {
            _javaWarpper.CallStatic(WrapperFuction.createRoom.ToString(),userName);
        }

        public void JoinChannel(int channelId)
        {
            _javaWarpper.CallStatic(WrapperFuction.joinChannel.ToString(), channelId);
        }

        public void ReJoinRoom(long roomId)
        {
            _javaWarpper.CallStatic(WrapperFuction.reJionRoom.ToString(), roomId);
        }

        public void SendTextMessage(string msg, int channelId, bool isTree)
        {
            _javaWarpper.CallStatic(WrapperFuction.sendChannelTextMessage.ToString(), msg, channelId, isTree);
        }


        public int OpenMic()
        {
            return _javaWarpper.CallStatic<int>(WrapperFuction.openMic.ToString());
        }

        public int CloseMic()
        {
            return _javaWarpper.CallStatic<int>(WrapperFuction.closeMic.ToString());
        }

        public int OpenSpeaker()
        {
            return _javaWarpper.CallStatic<int>(WrapperFuction.openSpeaker.ToString());
        }

        public int CloseSpeaker()
        {
            return _javaWarpper.CallStatic<int>(WrapperFuction.closeSpeaker.ToString());
        }

        public void DeallocMGAudio()
        {
            _javaWarpper.CallStatic<int>(WrapperFuction.destroy.ToString());
        }

        public int GetIsConnect()
        {
            return _javaWarpper.CallStatic<int>(WrapperFuction.getIsConnect.ToString());
        }

        public string GetMessage()
        {
            try{
                return _javaWarpper.CallStatic<string>(WrapperFuction.getMessage.ToString());
            }
            catch(Exception e){
                return null;
            }
           
        }
    }

}

